package com.tbit.main.controller;

import com.tbit.common.constant.OperationPlatform;
import com.tbit.common.entity.MachineOpLog;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.MachineAbnormalConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import com.tbit.main.service.impl.MachineOpLogService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * 头盔锁绑定
 *
 * @author LMY
 * @create 2022-09-22 15:33
 */
@RestController
@RequestMapping("/helmet")
public class HelmetController {

    @Autowired
    private HelmetService helmetService;
    @Autowired
    private DataService dataService;
    @Autowired
    private HelmetLogService helmetLogService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private MachineFunctionService machineFunctionService;
    @Autowired
    private HelmetBoundService helmetBoundService;
    @Autowired
    private MachineAbnormalService machineAbnormalService;
    @Autowired
    private HelmetAbnormalService helmetAbnormalService;
    @Autowired
    private MachineOpLogService machineOpLogService;

    /**
     * 查询
     *
     * @param userCode
     * @return
     */
    @RequestMapping("/geMachineNO")
    public JsonResult<Helmet> geMachineNO(String userCode) {
        Assert.notNull(userCode, "Machine.userCode.notNull");
        Integer machineId = dataService.getMachineIdUserCode(userCode);
        Helmet helmet = helmetBoundService.getMachineId(machineId);
        return JsonResult.succ(helmet);
    }

    /**
     * 绑定
     *
     * @param userCode
     * @param helmetMac
     * @return
     */
    @RequestMapping("/bound")
    @OperateLog(module = ModuleEnum.HELMET, methods = MethodEnum.HELMET_ADD)
    public JsonResult bound(String token, String userCode, String helmetMac) {
        Assert.notNull(userCode, "Machine.userCode.notNull");
        Assert.notNull(helmetMac, "helmet.mac.notnull");
        Integer machineId = dataService.getMachineIdUserCode(userCode);

        AccountUser accountUser = tokenService.getAccount(token);

        Machine machine = dataService.getMachine(machineId);

        Helmet helmet = helmetBoundService.getMachineId(machineId);
        Helmet helmetDB = helmetBoundService.getMac(helmetMac);

        if (helmet != null || helmetDB != null) {
            throw new BaseException("helmet.mac.bound");
        }

        MachineFunction machineFunction = machineFunctionService.getMachineIdHelmet(machineId);
        if (machineFunction == null) {
            throw new BaseException("machineFunction.type.notnull");
        }

        Helmet helmet1 = helmetService.getMac(helmetMac);
        if (helmet1 == null) {
            helmetService.insert(new Helmet(null, helmetMac, null, null, null, null, null));
        }

        helmetBoundService.insert(new HelmetBound(machineId, helmetMac, null));

        // 插入车辆操作日志
        machineOpLogService.insertSingle(MachineOpLog.builder()
                .accountId(machine.getAccountId())
                .machineId(machine.getMachineId())
                .machineNO(machine.getMachineNO())
                .userCode(machine.getUserCode())
                .operation("头盔设备")
                .opType("绑定")
                .opUser(accountUser.getName())
                .opUserPhone(accountUser.getPhone())
                .opPlatform(OperationPlatform.MAINTAIN.code)
                .detail("绑定头盔mac: " + helmetMac)
                .result(true)
                .opTime(LocalDateTime.now())
                .build()
        );

        return JsonResult.succ();
    }

    /**
     * 解绑
     *
     * @param token
     * @param userCode
     * @param remark
     * @return
     */
    @RequestMapping("/notBound")
    public JsonResult notBound(String token, String userCode, String reason, String remark) {

        Assert.notNull(userCode, "Machine.userCode.notNull");
        Integer machineId = dataService.getMachineIdUserCode(userCode);

        Machine machine = dataService.getMachine(machineId);

        Helmet helmet = helmetBoundService.getMachineId(machineId);
        if (helmet == null) {
            throw new BaseException("helmet.mac.notbound");
        }
        helmetBoundService.delId(machineId);

        machineAbnormalService.del(machineId, MachineAbnormalConstant.MACHINE_ABNORMAL__HELMET);
        helmetAbnormalService.delId(machineId);

        AccountUser accountUser = tokenService.getAccount(token);
        helmetLogService.insert(new HelmetLog(machineId, helmet.getHelmetMac(), reason, remark, DateTimeUtil.getNowTime(), accountUser.getAccountUserId()));

        // 插入车辆操作日志
        machineOpLogService.insertSingle(MachineOpLog.builder()
                .accountId(machine.getAccountId())
                .machineId(machine.getMachineId())
                .machineNO(machine.getMachineNO())
                .userCode(machine.getUserCode())
                .operation("头盔设备")
                .opType("解绑")
                .opUser(accountUser.getName())
                .opUserPhone(accountUser.getPhone())
                .opPlatform(OperationPlatform.MAINTAIN.code)
                .detail("解绑头盔mac: " + helmet.getHelmetMac())
                .result(true)
                .opTime(LocalDateTime.now())
                .build()
        );

        return JsonResult.succ();
    }

    /**
     * helmetMac
     *
     * @param token
     * @param helmetMac
     * @return
     */
    @RequestMapping("/bluetoothKey")
    public JsonResult<Helmet> bluetoothKey(String token, String helmetMac) {
        Assert.notNull(helmetMac, "helmet.mac.notnull");
        Helmet helmet = helmetService.getMac(helmetMac);
        return JsonResult.succ(helmet);
    }

}
